package com.aps.server.controller;

import com.aps.common.entity.Result;
import com.aps.server.dto.SchedulingPlanGenerateDto;
import com.aps.server.entity.ApsConstraint;
import com.aps.server.service.ApsConstraintService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * APS约束规则控制器
 * 提供约束规则相关的REST API接口
 */
@Slf4j
@RestController
@RequestMapping("/api/constraints")
@RequiredArgsConstructor
@Tag(name = "APS约束规则管理", description = "APS约束规则相关的API接口")
public class ApsConstraintController {

    @Resource
    ApsConstraintService apsConstraintService;

    /**
     * 创建约束规则
     */
    @PostMapping("create")
    @Operation(summary = "创建APS约束规则", description = "创建一个新的APS约束规则")
    public Result<String> createConstraint(@RequestBody ApsConstraint apsConstraint) {
        try {
            boolean success = apsConstraintService.createConstraint(apsConstraint);
            if (success) {
                return Result.success(apsConstraint.getId().toString(), "约束规则创建成功");
            } else {
                return Result.error(400, "约束规则创建失败");
            }
        } catch (Exception e) {
            log.error("创建APS约束规则失败", e);
            return Result.error(500, "创建约束规则失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID查询约束规则 详情
     */
    @GetMapping("/query")
    @Operation(summary = "根据ID查询约束规则详情", description = "根据约束规则ID查询约束规则信息（使用配置的租户ID）")
    public Result<SchedulingPlanGenerateDto> getConstraintById(
            @Parameter(description = "约束规则ID") @RequestParam("constraintId") String constraintId) {
        try {
            SchedulingPlanGenerateDto constraint = apsConstraintService.getTenantIdMapCacheInfo(constraintId);
            if(constraint != null) {
                return Result.success(constraint);
            }
            return Result.success(new SchedulingPlanGenerateDto());
        } catch (Exception e) {
            log.error("查询APS约束规则失败", e);
            return Result.error(500, "查询约束规则失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID和租户ID查询约束规则
     */
    @GetMapping("/{id}/tenant")
    @Operation(summary = "根据ID和租户ID查询约束规则", description = "根据约束规则ID和指定租户ID查询约束规则信息")
    public Result<ApsConstraint> getConstraintByIdWithTenant(
            @Parameter(description = "约束规则ID") @PathVariable String id,
            @Parameter(description = "租户ID") @RequestParam String tenantId) {
        try {
            ApsConstraint constraint = apsConstraintService.getConstraintById(id, tenantId);
            if (constraint != null) {
                return Result.success(constraint);
            } else {
                return Result.error(404, "约束规则未找到");
            }
        } catch (Exception e) {
            log.error("查询APS约束规则失败", e);
            return Result.error(500, "查询约束规则失败: " + e.getMessage());
        }
    }

    /**
     * 更新约束规则信息
     */
    @PostMapping("save")
    @Operation(summary = "更新约束规则信息", description = "根据约束规则ID更新约束规则信息")
    public Result<String> updateConstraint(@RequestBody SchedulingPlanGenerateDto dto) {
        try {
            boolean success = apsConstraintService.saveConstrainByConstrainIdDetails(dto);
            if (success) {
                return Result.success("约束规则更新成功");
            } else {
                return Result.error(400, "约束规则更新失败");
            }
        } catch (Exception e) {
            log.error("更新APS约束规则失败", e);
            return Result.error(500, "更新约束规则失败: " + e.getMessage());
        }
    }

    /**
     * 删除约束规则
     */
    @GetMapping("/del/{id}")
    @Operation(summary = "删除约束规则", description = "根据约束规则ID删除约束规则（使用配置的租户ID）")
    public Result<String> deleteConstraint(
            @Parameter(description = "约束规则ID") @PathVariable("id") String id) {
        try {
            boolean success = apsConstraintService.deleteConstraint(id);
            apsConstraintService.deleteConstraint(id);
            if (success) {
                return Result.success("约束规则删除成功");
            } else {
                return Result.error(400, "约束规则删除失败");
            }
        } catch (Exception e) {
            log.error("删除APS约束规则失败", e);
            return Result.error(500, "删除约束规则失败: " + e.getMessage());
        }
    }

    /**
     * 查询当前租户的约束规则列表
     */
    @GetMapping("/current-tenant")
    @Operation(summary = "查询当前租户的约束规则列表", description = "根据配置的租户ID查询所有约束规则")
    public Result<List<ApsConstraint>> getConstraintsByCurrentTenant() {
        try {
            List<ApsConstraint> constraints = apsConstraintService.getConstraintsByTenantId();
            return Result.success(constraints);
        } catch (Exception e) {
            log.error("根据当前租户ID查询约束规则失败", e);
            return Result.error(500, "查询约束规则失败: " + e.getMessage());
        }
    }
}